Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the number of translations even with null values #427

Merged
merged 1 commit into from
Dec 1, 2023

Conversation

sdebacker
Copy link
Contributor

@sdebacker sdebacker commented Nov 30, 2023

Currently, when saving this in the model:

['en' => 'Title', 'nl' => null, 'de' => null]

…we have this in database:

['en': 'Title', 'de': null]

This pull request fix this strange behavior and the data saved in the database is now:

['en' => 'Title', 'nl' => null, 'de' => null]

Note : In the version 3.1.3 of this package, the number of translations where correctly maintained in the database, I don't know exactly when this behavior changed, but it can lead to bugs in some situations.

@freekmurze freekmurze merged commit abecffc into spatie:main Dec 1, 2023
11 checks passed
@freekmurze
Copy link
Member

Thank you!

@mabdullahsari
Copy link
Contributor

mabdullahsari commented Dec 6, 2023

@freekmurze This is a behavioral change that broke production for us.

People using declare(strict_types=1) (like us) will be impacted.


Edit: if someone else encounters the same issue, just lock the version down to 6.5.3.

@@ -65,7 +65,7 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc

$translations = $this->getTranslations($key);

$translation = $translations[$normalizedLocale] ?? '';
$translation = $translations[$normalizedLocale] ?? null;
Copy link
Contributor

@mabdullahsari mabdullahsari Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big issue for str functions that no longer accept null values and consequently blow up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to change it back to an empty string. Do make sure all tests pass.

Copy link
Contributor Author

@sdebacker sdebacker Dec 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every nullable varchar string from the database have to be checked before using the str functions. Also, with the previous version, null values were saved in the json field for the last language, so this issue was already there, am I wrong ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous behavior was converting those default null values to empty strings so this check was handled by the package, which was then changed to be null moving this burden onto the shoulders of the package users. 👍

@poldixd
Copy link

poldixd commented Dec 6, 2023

That was also a critical change for us. Some of our tests in the CI failed.

We have to change

    expect($estate)
        ->getTranslation('headline', 'de', false)->toBe('')

to

    expect($estate)
        ->getTranslation('headline', 'de', false)->toBeNull()

@mabdullahsari
Copy link
Contributor

That was also a critical change for us. Some of our tests in the CI failed.

We have to change

    expect($estate)
        ->getTranslation('headline', 'de', false)->toBe('')

to

    expect($estate)
        ->getTranslation('headline', 'de', false)->toBeNull()

I've opened a PR that reverts this one. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants